﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Drawing;

namespace PicMerger
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        string[] _picturefiles = new string[8];
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            SelectPictureFile(0);
            if (!string.IsNullOrEmpty(_picturefiles[0]))
            {
                button2.IsEnabled = true;
            }
        }



        private void button2_Click(object sender, RoutedEventArgs e)
        {
            SelectPictureFile(1);
            if (!string.IsNullOrEmpty(_picturefiles[1]))
            {
                button3.IsEnabled = true;
                buttonMerge.IsEnabled = true;
            }
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            SelectPictureFile(2);
            if (!string.IsNullOrEmpty(_picturefiles[2]))
            {
                button4.IsEnabled = true;
            }
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            SelectPictureFile(3);
            if (!string.IsNullOrEmpty(_picturefiles[3]))
            {
                button5.IsEnabled = true;
            }
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            SelectPictureFile(4);
            if (!string.IsNullOrEmpty(_picturefiles[4]))
            {
                button6.IsEnabled = true;
            }
        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            SelectPictureFile(5);
            if (!string.IsNullOrEmpty(_picturefiles[5]))
            {
                button7.IsEnabled = true;
            }
        }

        private void button7_Click(object sender, RoutedEventArgs e)
        {
            SelectPictureFile(6);
            if (!string.IsNullOrEmpty(_picturefiles[6]))
            {
                button8.IsEnabled = true;
            }
        }

        private void button8_Click(object sender, RoutedEventArgs e)
        {
            SelectPictureFile(7);
        }

        private void MergeButton_Click(object sender, RoutedEventArgs e)
        {
            string resultfile = string.Empty;
            foreach (var file in _picturefiles)
            {
                if (string.IsNullOrEmpty(file)) break;
                if (string.IsNullOrEmpty(resultfile))
                {
                    resultfile = file;
                }
                else
                {
                    resultfile = MergePicture(resultfile, file);
                }
            }
        }

        public string MergePicture(string picturefile1, string picturefile2)
        {
            string mergedFilename = Guid.NewGuid().ToString() + ".jpg";
            Bitmap bit1 = new Bitmap(picturefile1);
            Bitmap bit2 = new Bitmap(picturefile2);
            Bitmap bit = new Bitmap((bit1.Width > bit2.Width) ? bit1.Width : bit2.Width, bit1.Height + bit2.Height);
            Graphics graph = Graphics.FromImage(bit);
            graph.DrawImage(bit1, 0, 0, bit1.Width, bit1.Height);
            graph.DrawImage(bit2, 0, bit1.Height, bit2.Width, bit2.Height);
            bit.Save(mergedFilename);

            return mergedFilename;
        }

        private void SelectPictureFile(int index)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            Nullable<bool> result = dlg.ShowDialog();

            if (result == true)
            {
                _picturefiles[index] = dlg.FileName;
            }
        }
    }
}
